package com.version1_3_1.dao.implement;

import com.version1_3_1.dao.ActivityDaoFixed;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by reeveslee on 16/4/21.
 */
@Repository
public class ActivityDaoFixedImpl implements ActivityDaoFixed {
    private static final Logger LOGGER = Logger.getLogger(ActivityDaoFixedImpl.class);
    @Autowired
    private JdbcTemplate jdbcTemplate;


    /**
     * 公益跑新的打卡信息
     *
     * @param userId
     * @return
     */
    @Override
    public Map getGongyipaoLaunchCardInfoFixed(int userId) {
        Map gongyipaoMap = new HashMap<>();

        try {
            String sql = "SELECT SUM(data1) AS mileCount FROM activity_launchrecord WHERE userId=? AND activityId=11";
            BigDecimal mileCount = jdbcTemplate.queryForObject(sql, new Object[]{userId}, java.math.BigDecimal.class);

            sql = "SELECT SUM(currData) AS timeCount FROM activity_launchrecord WHERE userId=? AND activityId=11 AND date=CURRENT_DATE";
            BigDecimal timeCount = jdbcTemplate.queryForObject(sql, new Object[]{userId}, java.math.BigDecimal.class);

            if (mileCount == null || timeCount == null) {
                return null;
            }

            gongyipaoMap.put("mileCount", mileCount);
            gongyipaoMap.put("timeCount", timeCount);
            return gongyipaoMap;
        } catch (Exception exception) {
            LOGGER.error("new gongyipao launch card info error", exception);
            return null;
        }
    }

    /**
     * 用户手动输入里程数
     *
     * @param mile
     * @param userId
     * @return
     */
    @Override
    public boolean inputMile(double mile, int userId) {
        try {
            String sql = "SELECT data1 FROM activity_launchrecord WHERE userId=? AND date=CURRENT_DATE AND activityId=11";
            BigDecimal mileDayCount = jdbcTemplate.queryForObject(sql, new Object[]{userId}, java.math.BigDecimal.class);

            if (mile + mileDayCount.doubleValue() > 30) {
                mile = 30 - mileDayCount.doubleValue();

                sql = "UPDATE activity_launchrecord SET data1=data1+? WHERE userId=? AND date=CURRENT_DATE AND activityId=11";
                jdbcTemplate.update(sql, new Object[]{mile, userId});
                return false;
            }
        } catch (Exception exception) {
        }

        String sql = "UPDATE activity_launchrecord SET data1=data1+? WHERE userId=? AND date=CURRENT_DATE AND activityId=11";
        int row = jdbcTemplate.update(sql, new Object[]{mile, userId});
        return (row == 1) ? true : false;
    }

    /**
     * 获取公益跑当日里程
     *
     * @param userId
     * @return
     */
    @Override
    public BigDecimal getGongyipaoTodayMile(int userId) {
        String sql = "SELECT data1 FROM activity_launchrecord WHERE userId=? AND date=CURRENT_DATE AND activityId=11";
        return jdbcTemplate.queryForObject(sql, new Object[]{userId}, java.math.BigDecimal.class);
    }
}
